package ae.pcfc.etks.online.itworx.ehs.web.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

import ae.pcfc.etks.online.itworx.ehs.web.model.FNP;

public class MarshallingUtility {

	private static String xmlLocation;
	private static Jaxb2Marshaller marshaller;
	
	private static Logger log = LoggerFactory.getLogger(MarshallingUtility.class);

	public void setXmlLocation(String xmlFileLocation) {
		xmlLocation = xmlFileLocation;
	}

	public void setMarshaller(Jaxb2Marshaller jaxbMarshaller) {
		marshaller = jaxbMarshaller;
	}

	public static boolean marshallFNP(FNP fnp) {
		
		try {
			FileOutputStream outputStream = new FileOutputStream(new File(
					xmlLocation));
			StreamResult result = new StreamResult(outputStream);

			marshaller.marshal(fnp, result);

		} catch (FileNotFoundException exp) {
			// TODO : change this
			log.error("MarshallingUtility.marshallFNP() - file not found for path ("+xmlLocation+") ",exp);
			return false;
		}
		return true;
	}

	public static FNP unmarshallFNP() {

		FNP fnp = null;
		try {
			FileInputStream inputStream = new FileInputStream(new File(
					xmlLocation));
			StreamSource source = new StreamSource(inputStream);

			fnp = (FNP) marshaller.unmarshal(source);
		} catch (FileNotFoundException exp) {
			// TODO : change this
			log.error("MarshallingUtility.unmarshallFNP() - file not found for path ("+xmlLocation+") ",exp);
		}
		return fnp;
	}
}
